<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" class="formWidth" label-width="90px">
      <el-form-item label="姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="民族" prop="nationality">
        <el-input
          v-model="queryParams.nationality"
          placeholder="请输入民族"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="性别" prop="sex">
        <el-select v-model="queryParams.sex" placeholder="请选择性别" clearable size="small">
          <el-option
            v-for="dict in sexOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item> -->
      <el-form-item label="出生日期" prop="birthDate">
        <el-date-picker clearable size="small"
        :picker-options="pickerOptions"
          v-model="queryParams.birthDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择出生日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="身份证号" prop="idCard">
        <el-input
          v-model="queryParams.idCard"
          placeholder="请输入身份证号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="户籍所在地" prop="census">
        <el-input
          v-model="queryParams.census"
          placeholder="请输入户籍所在地"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="签发机关" prop="signOrg">
        <el-input
          v-model="queryParams.signOrg"
          placeholder="请输入签发机关"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="有效期" prop="validPeriod">
        <el-input
          v-model="queryParams.validPeriod"
          placeholder="请输入有效期"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="政治面貌" prop="politicsStatus">
        <el-select v-model="queryParams.politicsStatus" placeholder="请选择政治面貌" clearable size="small">
          <el-option
            v-for="dict in politicsStatusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="民主党派" prop="democraticParty">
        <el-select v-model="queryParams.democraticParty" placeholder="请选择民主党派" clearable size="small">
          <el-option
            v-for="dict in democraticPartyOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="文化程度" prop="educationalLevel">
        <el-select v-model="queryParams.educationalLevel" placeholder="请选择文化程度" clearable size="small">
          <el-option
            v-for="dict in educationalLevelOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="户籍类别" prop="censusType">
        <el-select v-model="queryParams.censusType" placeholder="请选择户籍类别" clearable size="small">
          <el-option
            v-for="dict in censusTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="婚姻状况" prop="marryType">
        <el-select v-model="queryParams.marryType" placeholder="请选择婚姻状况" clearable size="small">
          <el-option
            v-for="dict in marryTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['archive_info:base:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['archive_info:base:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['archive_info:base:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['archive_info:base:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="baseList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="姓名" align="center" prop="name" />
      <el-table-column label="身份证号" :show-overflow-tooltip="true" align="center" prop="idCard"  width="190" :formatter="idNumberFormat"/>
       <el-table-column label="民族" align="center" prop="nationality" />
      <el-table-column label="性别" align="center" prop="sex" :formatter="sexFormat" />
      <el-table-column label="出生日期" align="center" prop="birthDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.birthDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="婚姻状况" align="center" prop="marryType" :formatter="marryTypeFormat"/>
      <el-table-column label="户籍所在地" align="center" prop="census" />
      <el-table-column label="签发机关" align="center" prop="signOrg" />
      <el-table-column label="有效期" align="center" prop="validPeriod" />
      <el-table-column label="政治面貌" align="center" prop="politicsStatus" :formatter="politicsStatusFormat" />
      <el-table-column label="民主党派" align="center" prop="democraticParty" :formatter="democraticPartyFormat" />
      <el-table-column label="文化程度" align="center" prop="educationalLevel" :formatter="educationalLevelFormat" />
      <el-table-column label="户籍类别" align="center" prop="censusType" :formatter="censusTypeFormat" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-folder-add"
            @click="handleOtherAdd(scope.row)"
            v-hasPermi="['archive_info:base:otherAdd']"
          >补充信息</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['archive_info:base:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['archive_info:base:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改军人基本信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form class="formWidth" ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="姓名" prop="name">
              <el-input v-model="form.name" placeholder="请输入姓名" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="民族" prop="nationality">
              <el-input v-model="form.nationality" placeholder="请输入民族" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="性别" prop="sex">
              <el-select v-model="form.sex" placeholder="请选择性别">
                <el-option
                  v-for="dict in sexOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="parseInt(dict.dictValue)"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="出生日期" prop="birthDate">
              <el-date-picker clearable size="small"
              :picker-options="pickerOptions"
                v-model="form.birthDate"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="选择出生日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="身份证号" prop="idCard">
              <el-input v-model="form.idCard" placeholder="请输入身份证号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="户籍所在地" prop="census">
              <el-input v-model="form.census" placeholder="请输入户籍所在地" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="签发机关" prop="signOrg">
              <el-input v-model="form.signOrg" placeholder="请输入签发机关" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="有效期" prop="validPeriod">
              <el-input v-model="form.validPeriod" placeholder="请输入有效期" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="政治面貌" prop="politicsStatus">
              <el-select v-model="form.politicsStatus" placeholder="请选择政治面貌" @change="changeParty">
                <el-option
                  v-for="dict in politicsStatusOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="parseInt(dict.dictValue)"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="民主党派" prop="democraticParty">
              <el-select v-model="form.democraticParty" placeholder="请选择民主党派" :disabled="form.politicsStatus != 2">
                <el-option
                  v-for="dict in democraticPartyOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="parseInt(dict.dictValue)"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="文化程度" prop="educationalLevel">
              <el-select v-model="form.educationalLevel" placeholder="请选择文化程度">
                <el-option
                  v-for="dict in educationalLevelOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="parseInt(dict.dictValue)"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="户籍类别" prop="censusType">
              <el-select v-model="form.censusType" placeholder="请选择户籍类别">
                <el-option
                  v-for="dict in censusTypeOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="parseInt(dict.dictValue)"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="婚姻状况" prop="marryType">
              <el-select v-model="form.marryType" placeholder="请选择婚姻状况">
                <el-option
                  v-for="dict in marryTypeOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="parseInt(dict.dictValue)"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系方式" prop="contactInfo">
              <el-select v-model="form.contactInfo" placeholder="请选择联系方式">
                <el-option
                  v-for="dict in contactInfoOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="parseInt(dict.dictValue)"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="手机号" prop="phoneNumber">
              <el-input v-model="form.phoneNumber" placeholder="请输入手机号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="固话" prop="fixedTelephone">
              <el-input v-model="form.fixedTelephone" placeholder="如填写，区号必填" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="QQ或微信号" prop="networkNumber">
              <el-input v-model="form.networkNumber" placeholder="请输入QQ或微信号" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listBase, getBase, delBase, addBase, updateBase, exportBase } from "@/api/archive_info/base";

export default {
  name: "Base",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 军人基本信息表格数据
      baseList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 性别字典
      sexOptions: [],
      // 政治面貌字典
      politicsStatusOptions: [],
      // 民主党派字典
      democraticPartyOptions: [],
      // 文化程度字典
      educationalLevelOptions: [],
      // 户籍类别字典
      censusTypeOptions: [],
      // 婚姻状况字典
      marryTypeOptions: [],
      // 联系方式字典
      contactInfoOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        nationality: null,
        sex: null,
        birthDate: null,
        idCard: null,
        census: null,
        signOrg: null,
        validPeriod: null,
        politicsStatus: null,
        democraticParty: null,
        educationalLevel: null,
        censusType: null,
        marryType: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "姓名不能为空", trigger: "blur" }
        ],
        nationality: [
          { required: true, message: "民族不能为空", trigger: "blur" }
        ],
        sex: [
          { required: true, message: "性别不能为空", trigger: "change" }
        ],
        birthDate: [
          { required: true, message: "出生日期不能为空", trigger: "blur" }
        ],
        idCard: [
          { required: true, message: "身份证号不能为空", trigger: "blur" }
        ],
        signOrg: [
          { required: true, message: "签发机关不能为空", trigger: "blur" }
        ],
        validPeriod: [
          { required: true, message: "有效期不能为空", trigger: "blur" }
        ],
        politicsStatus: [
          { required: true, message: "政治面貌不能为空", trigger: "change" }
        ],
        censusType: [
          { required: true, message: "户籍类别不能为空", trigger: "change" }
        ],
        marryType: [
          { required: true, message: "婚姻状况不能为空", trigger: "change" }
        ],
        contactInfo: [
          { required: true, message: "联系方式不能为空", trigger: "change" }
        ],
      },
       pickerOptions:{
         disabledDate(time) {
            return time.getTime() > Date.now() - 8.64e6;//如果没有后面的-8.64e6就是不可以选择今天的
          }
      },
      isSelect: false,
    };
  },
  created() {
    this.getList();
    this.getDicts("s_sex").then(response => {
      this.sexOptions = response.data;
    });
    this.getDicts("political_status").then(response => {
      this.politicsStatusOptions = response.data;
    });
    this.getDicts("democratic_party").then(response => {
      this.democraticPartyOptions = response.data;
    });
    this.getDicts("degree_of_education").then(response => {
      this.educationalLevelOptions = response.data;
    });
    this.getDicts("enlisted_census_type").then(response => {
      this.censusTypeOptions = response.data;
    });
    this.getDicts("marry_type").then(response => {
      this.marryTypeOptions = response.data;
    });
    this.getDicts("contact_info").then(response => {
      this.contactInfoOptions = response.data;
    });
  },
  methods: {
    /** 查询军人基本信息列表 */
    getList() {
      this.loading = true;
      listBase(this.queryParams).then(response => {
        this.baseList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 性别字典翻译
    sexFormat(row, column) {
      return this.selectDictLabel(this.sexOptions, row.sex);
    },
    // 政治面貌字典翻译
    politicsStatusFormat(row, column) {
      return this.selectDictLabel(this.politicsStatusOptions, row.politicsStatus);
    },
    // 民主党派字典翻译
    democraticPartyFormat(row, column) {
      return this.selectDictLabel(this.democraticPartyOptions, row.democraticParty);
    },
    // 文化程度字典翻译
    educationalLevelFormat(row, column) {
      return this.selectDictLabel(this.educationalLevelOptions, row.educationalLevel);
    },
    // 户籍类别字典翻译
    censusTypeFormat(row, column) {
      return this.selectDictLabel(this.censusTypeOptions, row.censusType);
    },
    // 婚姻状况字典翻译
    marryTypeFormat(row, column) {
      return this.selectDictLabel(this.marryTypeOptions, row.marryType);
    },
    // 联系方式字典翻译
    contactInfoFormat(row, column) {
      return this.selectDictLabel(this.contactInfoOptions, row.contactInfo);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        nationality: null,
        sex: null,
        birthDate: null,
        idCard: null,
        photoUrl: null,
        census: null,
        censusProvince: null,
        censusCity: null,
        censusArea: null,
        signOrg: null,
        validPeriod: null,
        politicsStatus: null,
        democraticParty: null,
        educationalLevel: null,
        addressLiving: null,
        addressLivingProvince: null,
        addressLivingCity: null,
        addressLivingArea: null,
        addressLivingTown: null,
        censusType: null,
        householdPhoto: null,
        recentPhoto: null,
        marryType: null,
        contactInfo: null,
        phoneNumber: null,
        fixedTelephone: null,
        networkNumber: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加军人基本信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getBase(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改军人基本信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateBase(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBase(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除军人基本信息编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delBase(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有军人基本信息数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportBase(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    },
    /** 列表身份证格式 */
    idNumberFormat(row,column){
      return row.idCard.replace(/^(.{6})(?:\w+)(.{4})$/, "\$1********\$2")
    },
    /** 添加修改中：民主党派根据政治面貌选择 */
    changeParty(value){
      if(value != 2){
        this.form.democraticParty = ""; 
      }
    },
    // 补充信息
    handleOtherAdd(row){
      const id = row.id;
      this.$router.push("/soldier_infomation/other-info/index/" + row.id + '/' + row.name + '/' + row.idCard);
    }
  }
};
</script>